package so.sao.code.service.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import so.sao.code.service.domain.entity.TagBatch;
import so.sao.code.service.infrastructure.enums.TAG_BATCH_TYPE;

@Repository
public interface TagBatchDao extends JpaRepository<TagBatch, Long> {


    @Query(value = "select sum(tag_count) from tag_batch where ese_id = ?1", nativeQuery = true)
    Long sumTagCountByEseId(long eseId);

    List<TagBatch> findByEseIdAndTypeAndTagBatchConfigIdNot(long eseId, TAG_BATCH_TYPE type, long config);


    @Query(value = "select TOP 1 tb.* from tag_batch tb,tag_batch_config tbc where tb.tag_batch_config_id = tbc.id and tb.ese_id = ?1 and tb.print_file_status = ?2 and tbc.download_method = ?3 ORDER BY tb.id ASC", nativeQuery = true)
    TagBatch findTopByEseIdAndPrintFileStatusAndDownloadMethodOrderByIdAsc(long eseId, Integer printFileStatus,
                                                                           Integer downloadMethod);

    Page<TagBatch> findAll(Specification<TagBatch> spec, Pageable pageable);

    long countBatchByEseId(long eseId);

    /**
     * 获取企业已生成码数
     */
    @Query(value = "select sum(tag_count) from tag_batch where ese_id = ?1", nativeQuery = true)
    Long sumCodeByEseId(long eseId);


    TagBatch findByIdAndCreateUserId(Long id, Long userId);


    @Query(value = "select sum(tag_count) from tag_batch", nativeQuery = true)
    Long sumCode();

    /**
     * 通过一个码序列号获取批次信息Entity
     *
     * @param seqNum 码序列号
     * @return
     */
    @Query(value = "select top 1 * from tag_batch where ?1 between seqnum_start and seqnum_end", nativeQuery = true)
    TagBatch getTagBatchBySeqNum(long seqNum);

	 List<TagBatch>  findAll(Specification<TagBatch> spec);

	List<TagBatch> findByIdIn(List<Long> ids);

	List<TagBatch> findByTypeAndIdIn(TAG_BATCH_TYPE type, List<Long> tagBatchIds);

}
